package cn.danger.com.db;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import cn.bmob.v3.datatype.BmobDate;
import cn.danger.com.model.entity.Schedule;
import cn.danger.com.util.DateUtil;
import cn.danger.com.util.FileCacheService;

/**
 * Created by 俊 on 2017/1/2.
 */
public class ScheduleDaoImpl implements ScheduleDao {

    private DBHelper helper;
    private Context context;
    private static final String SQL_INSERT = "insert into schedule(objectId,username,content,date_start," +
            "type,remind,remind_time,remind_ring) values(?,?,?,?,?,?,?,?)";
    private final static String SQL_DELETE = "delete from schedule where id = ?";
    private final static String SQL_UPDATE = "update schedule set username = ?, objectId = ?, content = ?, date_start = ?, " +
            "type = ?, remind = ?, remind_time = ?, remind_ring = ? where id = ?";
    private final static String SQL_QUERY = "select * from schedule where username = ?";
    private final static String SQL_QUERY_SCHEDULE = "select * from schedule where id = ?";
    private final static String SQL_QUERY_ID = "select max(id) from schedule";

    public ScheduleDaoImpl(Context context) {
        this.context = context;
        helper = DBHelper.getInstance(context);
    }

    @Override
    public int insert(Schedule schedule) {
        SQLiteDatabase db = helper.getWritableDatabase();
        db.execSQL(SQL_INSERT, new Object[]{schedule.getObjectId(), schedule.getUsername(), schedule.getContent(),
                schedule.getDate_start(), schedule.getType(), schedule.getRemind(),
                schedule.getRemind_time(), schedule.getRemind_ring()});
        Cursor cursor = db.rawQuery(SQL_QUERY_ID, null);
        int id = -1;
        if (cursor.moveToNext()) {
            id = cursor.getInt(0);
        }

        cursor.close();
        db.close();
        return id;
    }

    @Override
    public void delete(int id) {
        SQLiteDatabase db = helper.getWritableDatabase();
        db.execSQL(SQL_DELETE, new Object[]{id});
    }

    @Override
    public void update(Schedule schedule) {
        SQLiteDatabase db = helper.getWritableDatabase();
        db.execSQL(SQL_UPDATE, new Object[]{schedule.getUsername(), schedule.getObjectId(), schedule.getContent(),
                schedule.getDate_start(), schedule.getType(), schedule.getRemind(),
                schedule.getRemind_time(), schedule.getRemind_ring(), schedule.getId()});
        db.close();
    }

    @Override
    public Schedule getSchedule(int id) {
        SQLiteDatabase db = helper.getWritableDatabase();
        Schedule schedule = new Schedule();
        Cursor cursor = db.rawQuery(SQL_QUERY_SCHEDULE, new String[]{id + ""});
        if (cursor.moveToNext()) {
            schedule.setId(id);
            schedule.setObjectId(cursor.getString(cursor.getColumnIndex("objectId")));
            schedule.setUsername(cursor.getString(cursor.getColumnIndex("username")));
            schedule.setContent(cursor.getString(cursor.getColumnIndex("content")));
            schedule.setDate_start(cursor.getString(cursor.getColumnIndex("date_start")));
            schedule.setType(cursor.getString(cursor.getColumnIndex("type")));
            schedule.setRemind(cursor.getInt(cursor.getColumnIndex("remind")));
            schedule.setRemind_time(cursor.getString(cursor.getColumnIndex("remind_time")));
            schedule.setRemind_ring(cursor.getString(cursor.getColumnIndex("remind_ring")));
        }
        cursor.close();
        db.close();
        return schedule;
    }

    @Override
    public Schedule getWillArriveSchedule(String username) {
        SQLiteDatabase db = helper.getWritableDatabase();
        Schedule schedule = new Schedule();
        Cursor cursor = db.rawQuery(SQL_QUERY, new String[]{username});
        while (cursor.moveToNext()) {
            String time = cursor.getString(cursor.getColumnIndex("date_start"));
            String date = new BmobDate(new Date(time)).getDate();
            String today = new BmobDate(new Date(System.currentTimeMillis())).getDate().split(" ")[0];
            if (date.split(" ")[0].equals(today)) {
                if (Long.parseLong(time) > System.currentTimeMillis()) {
                    schedule.setId(cursor.getInt(cursor.getColumnIndex("id")));
                    schedule.setObjectId(cursor.getString(cursor.getColumnIndex("objectId")));
                    schedule.setUsername(username);
                    schedule.setContent(cursor.getString(cursor.getColumnIndex("content")));
                    schedule.setDate_start(cursor.getString(cursor.getColumnIndex("date_start")));
                    schedule.setType(cursor.getString(cursor.getColumnIndex("type")));
                    schedule.setRemind(cursor.getInt(cursor.getColumnIndex("remind")));
                    schedule.setRemind_time(cursor.getString(cursor.getColumnIndex("remind_time")));
                    schedule.setRemind_ring(cursor.getString(cursor.getColumnIndex("remind_ring")));
                    break;
                }
            }
        }
        cursor.close();
        db.close();
        return schedule;
    }

    @Override
    public List<Integer> getMonthSchedule(String username, String year, String month) {
        SQLiteDatabase db = helper.getWritableDatabase();
        List<Integer> tasks = new ArrayList<>();
        Cursor cursor = db.rawQuery(SQL_QUERY, new String[]{username});
        while (cursor.moveToNext()) {
            String date = new BmobDate(new Date(Long.parseLong(cursor.getString(cursor.getColumnIndex("date_start"))))).getDate().split(" ")[0];
            String origin_year = date.split("-")[0];
            String origin_month = date.split("-")[1];
            String origin_day = date.split("-")[2];
            if ((origin_year + "-" + origin_month).equals(year + "-" + DateUtil.getMonth(month))) {
                tasks.add(Integer.parseInt(origin_day));
            }
        }
        cursor.close();
        db.close();
        return tasks;
    }

    @Override
    public List<Schedule> getTodaySchedule(String username) {
        SQLiteDatabase db = helper.getWritableDatabase();
        List<Schedule> schedules = new ArrayList<>();
        Cursor cursor = db.rawQuery(SQL_QUERY, new String[]{username});
        Map<String, Object> map = (Map<String, Object>) new FileCacheService(context).getSharePreference("setting");
        boolean show_finished_event = (boolean) map.get("show_finished_event");
        while (cursor.moveToNext()) {
            String date = new BmobDate(new Date(Long.parseLong(cursor.getString(cursor.getColumnIndex("date_start"))))).getDate();
            String today = new BmobDate(new Date(System.currentTimeMillis())).getDate().split(" ")[0];
            if (date.split(" ")[0].equals(today)) {
                if (show_finished_event) {
                    Schedule schedule = new Schedule();
                    schedule.setId(cursor.getInt(cursor.getColumnIndex("id")));
                    schedule.setObjectId(cursor.getString(cursor.getColumnIndex("objectId")));
                    schedule.setUsername(username);
                    schedule.setContent(cursor.getString(cursor.getColumnIndex("content")));
                    schedule.setDate_start(cursor.getString(cursor.getColumnIndex("date_start")));
                    schedule.setType(cursor.getString(cursor.getColumnIndex("type")));
                    schedule.setRemind(cursor.getInt(cursor.getColumnIndex("remind")));
                    schedule.setRemind_time(cursor.getString(cursor.getColumnIndex("remind_time")));
                    schedule.setRemind_ring(cursor.getString(cursor.getColumnIndex("remind_ring")));
                    schedules.add(schedule);
                } else {
                    if (System.currentTimeMillis() < Long.parseLong(cursor.getString(cursor.getColumnIndex("date_start")))) {
                        Schedule schedule = new Schedule();
                        schedule.setId(cursor.getInt(cursor.getColumnIndex("id")));
                        schedule.setObjectId(cursor.getString(cursor.getColumnIndex("objectId")));
                        schedule.setUsername(username);
                        schedule.setContent(cursor.getString(cursor.getColumnIndex("content")));
                        schedule.setDate_start(cursor.getString(cursor.getColumnIndex("date_start")));
                        schedule.setType(cursor.getString(cursor.getColumnIndex("type")));
                        schedule.setRemind(cursor.getInt(cursor.getColumnIndex("remind")));
                        schedule.setRemind_time(cursor.getString(cursor.getColumnIndex("remind_time")));
                        schedule.setRemind_ring(cursor.getString(cursor.getColumnIndex("remind_ring")));
                        schedules.add(schedule);
                    }
                }
            }
        }
        cursor.close();
        db.close();
        return schedules;
    }

    @Override
    public List<Schedule> getDaySchedule(String username, String day) {
        SQLiteDatabase db = helper.getWritableDatabase();
        List<Schedule> schedules = new ArrayList<>();
        Cursor cursor = db.rawQuery(SQL_QUERY, new String[]{username});
        while (cursor.moveToNext()) {
            String date = new BmobDate(new Date(Long.parseLong(cursor.getString(cursor.getColumnIndex("date_start"))))).getDate();
            if (date.split(" ")[0].equals(day)) {
                Schedule schedule = new Schedule();
                schedule.setId(cursor.getInt(cursor.getColumnIndex("id")));
                schedule.setObjectId(cursor.getString(cursor.getColumnIndex("objectId")));
                schedule.setUsername(username);
                schedule.setContent(cursor.getString(cursor.getColumnIndex("content")));
                schedule.setDate_start(cursor.getString(cursor.getColumnIndex("date_start")));
                schedule.setType(cursor.getString(cursor.getColumnIndex("type")));
                schedule.setRemind(cursor.getInt(cursor.getColumnIndex("remind")));
                schedule.setRemind_time(cursor.getString(cursor.getColumnIndex("remind_time")));
                schedule.setRemind_ring(cursor.getString(cursor.getColumnIndex("remind_ring")));
                schedules.add(schedule);
            }
        }
        cursor.close();
        db.close();
        return schedules;
    }

    @Override
    public List<Schedule> getSchedules(String username) {
        SQLiteDatabase db = helper.getWritableDatabase();
        List<Schedule> schedules = new ArrayList<>();
        Cursor cursor = db.rawQuery(SQL_QUERY, new String[]{username});
        while (cursor.moveToNext()) {
            Schedule schedule = new Schedule();
            schedule.setId(cursor.getInt(cursor.getColumnIndex("id")));
            schedule.setObjectId(cursor.getString(cursor.getColumnIndex("objectId")));
            schedule.setUsername(username);
            schedule.setContent(cursor.getString(cursor.getColumnIndex("content")));
            schedule.setDate_start(cursor.getString(cursor.getColumnIndex("date_start")));
            schedule.setType(cursor.getString(cursor.getColumnIndex("type")));
            schedule.setRemind(cursor.getInt(cursor.getColumnIndex("remind")));
            schedule.setRemind_time(cursor.getString(cursor.getColumnIndex("remind_time")));
            schedule.setRemind_ring(cursor.getString(cursor.getColumnIndex("remind_ring")));
            schedules.add(schedule);
        }
        cursor.close();
        db.close();
        return schedules;
    }
}
